package com.weike.wk_android.presenter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.weike.wk_android.R;
import com.weike.wk_android.contract.LoginContract;
import com.weike.wk_android.model.UserInfo;
import com.weike.wk_android.utils.RetrofitUtils;
import com.weike.wk_android.utils.RxHelper;
import com.weike.wk_android.utils.Utils;
import com.weike.wk_android.view.activity.MainActivity;
import com.weike.wk_android.widget.toast.MyToast;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginPresenter implements LoginContract.Presenter {
    Context context;
    private LoginContract.View view;

    public LoginPresenter(Context context, LoginContract.View view) {
        this.context = context;
        this.view = view;
        view.setPresenter(this);
    }

    @Override
    public void login(String username, String password) {
        // 模拟获取数据信息绑定
       RetrofitUtils.getApi().loginWithRxJava(Utils.replace(username), password)
                .compose(RxHelper.observableIO2Main(this.context))
                .subscribe(new Observer<UserInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        // 显示加载框
                        view.showLoading();
                    }

                    @Override
                    public void onNext(UserInfo userInfo) {
                        view.showLoginInfo(userInfo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.dismissLoading();
                    }

                    @Override
                    public void onComplete() {
                        view.dismissLoading();
                    }
                });
    }

    /**
     *
     */
    @Override
    public void start() {}
}
